﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
using WebUtil;
public partial class admin_OrderDetailAdd : SitePage
{
    #region Fields

    busStoreOrder _busOrder = new busStoreOrder();
    busStoreOrderDetail _busOrderDetail = new busStoreOrderDetail();
    busEntry _busEntry = new busEntry();
    public string requestQty
    {

        get
        {
            object o = ViewState["requestQty"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["requestQty"] = value; }

    }
    public int OrderDetailId
    {

        get
        {
            object o = ViewState["OrderDetailId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["OrderDetailId"] = value; }

    }
    public int LocationId
    {

        get
        {
            object o = ViewState["LocationId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["LocationId"] = value; }

    }
    public int EntryId
    {

        get
        {
            object o = ViewState["EntryId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["EntryId"] = value; }

    }

    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.OrderDetailId = clsQueryString.GetInt("OrderDetailId");
            this.EntryId = clsQueryString.GetInt("EntryId");
            this.LocationId = clsQueryString.GetInt("LocationId");
            Common.BindLocation(drpLocation, true);
            Common.BindShift(drpShift, true);
            Common.BindUSer(drpProducerId, true);
            Common.BindUSer(drpSupervisor, true);
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    
    private void LoadData()
    {
        int currentEnrtyQty = 0;
        //load Entry
        if (this.EntryId > 0)
        {
            Entry objEntry = _busEntry.GetByID(this.EntryId);
            if (objEntry != null)
            {
                if (objEntry.ProducedDate != DateTime.MinValue)
                    txtProducedDate.Text = clsConvert.String(objEntry.ProducedDate);
                txtQuantity.Text = objEntry.Quantity.ToString();
                drpLocation.SelectedValue = objEntry.LocationId.ToString();
                drpProducerId.SelectedValue = objEntry.ProducerId.ToString();
                drpShift.SelectedValue = objEntry.ShiftId.ToString();
                drpSupervisor.SelectedValue = objEntry.SupervisorId.ToString();

                currentEnrtyQty = objEntry.Quantity;
            }
        }
        else
        {
            drpLocation.SelectedValue = this.LocationId.ToString();
        }
        btnSave.Visible = false;
        if (this.OrderDetailId > 0)
        {
            StoreOrderDetail objOrderDetail = _busOrderDetail.GetByID(this.OrderDetailId);
            if (objOrderDetail != null)
            {
                btnSave.Visible = true;
                txtDesignCode.Text = objOrderDetail.DesignCode;
                if (objOrderDetail.Quantity > 0)
                    requestQty = objOrderDetail.Quantity.ToString("###,###,###");
                else requestQty = "0";
                //count qty available
                clsSQL sql = new clsSQL();
                int count = (int)sql.ExecuteScalar("Select COALESCE(sum(Quantity),0) from Entry where OrderDetailId=" + OrderDetailId.ToString() + " and LocationId=" + this.LocationId);
                int available = objOrderDetail.Quantity - count + currentEnrtyQty;
                if (available < 0)
                    available = 0;
                txtOrderDetailQuantity.Text = available.ToString();
                if (drpLocation.SelectedValue == WebUtil.Common.LocationThietKe.ToString())
                {
                    txtQuantity.Text = objOrderDetail.Quantity.ToString();
                    txtQuantity.Enabled = false;
                }
            }
        }

    }
    private void GoList()
    {
        string backURL = clsQueryString.GetString("backURL");
        if (string.IsNullOrEmpty(backURL))
        {
            backURL = "EntryList.aspx?OrderDetailId=" + this.OrderDetailId + "&LocationId=" + this.LocationId;
        }
        Response.Redirect(backURL);
    }
    private void UpdateEntry(Entry objData)
    {

        objData.EntryId = this.EntryId;
        bool result = _busEntry.Update(objData);
        if (result)
            GoList();

        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertEntry(Entry objData)
    {
        int result = _busEntry.Insert(objData);
        if (result > 0)
            GoList();
        else
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int qtyAvailable = clsConvert.Int(txtOrderDetailQuantity.Text);
            int qty = clsConvert.Int(txtQuantity.Text.Trim());
            if (qty <= 0)
            {
                lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Số lượng thực hiện");
                return;
            }
            if (qty > qtyAvailable)
            {
                lblErrorMessage.Text = string.Format(Resources.Resource.MSG_COMPARE_LESS_THAN, "Số lượng thực hiện", qtyAvailable.ToString());
                return;
            }
            DateTime producedDate = clsConvert.Date(txtProducedDate.Text);
            if (!string.IsNullOrEmpty(txtProducedDate.Text))
            {
                if (producedDate == DateTime.MinValue)
                {
                    lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Ngày sản xuất");
                    return;
                }
            }
            //get datavalue
            Entry objData = new Entry();
            objData.OrderDetailId = this.OrderDetailId;
            objData.Quantity = qty;
            objData.LocationId = clsConvert.Int(drpLocation.SelectedValue);
            objData.ProducedDate = producedDate;
            if (!string.IsNullOrEmpty(drpProducerId.SelectedValue))
                objData.ProducerId = new Guid(drpProducerId.SelectedValue);
            else objData.ProducerId = Guid.Empty;
            objData.ShiftId = clsConvert.Int(drpShift.SelectedValue);
            if (!string.IsNullOrEmpty(drpSupervisor.SelectedValue))
                objData.SupervisorId = new Guid(drpSupervisor.SelectedValue);
            else
                objData.SupervisorId = Guid.Empty;
            objData.CreatedBy = this.userloginName;
            objData.CreatedDate = clsDateTime.Now;
            objData.ModifiedBy = this.userloginName;
            objData.ModifiedDate = clsDateTime.Now;
            if (this.EntryId > 0)
                UpdateEntry(objData);
            else
                InsertEntry(objData);
        }
        else
        {
            vsumTipNew.ShowMessageBox = false;
            vsumTipNew.ShowSummary = true;
        }
    }

    protected void btnCacel_Click(object sender, EventArgs e)
    {
        GoList();
    }
  

    #endregion


}